package com.yz.controller.admin;

import com.yz.pojo.Tag;
import com.yz.service.TagService;
import com.yz.util.Result;
import com.yz.util.ResultGenerator;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private TagService tagService;

    @RequestMapping("/tag")
    public String tag(Model model){
        List<Tag> tags = tagService.queryAllTags();
        model.addAttribute("tags",tags);
        return "admin/tag";
    }

    @RequestMapping("/tag/save")
    @ResponseBody
    public Result save(@RequestParam("name") String name){
        if (Strings.isEmpty(name)){
            return ResultGenerator.genFailResult("请输入标签名称");
        }
        if(tagService.insertTag(name) > 0){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genFailResult("标签名称重复");
        }
    }

    @RequestMapping("/tag/delete")
    @ResponseBody
    public Result delete(@RequestParam("ids") Integer[] ids){
        if(tagService.deleteTag(ids) > 0){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genFailResult("删除失败");
        }
    }

}
